<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login extends MY_Controller {

    public function index() {
        $this->_data["menuHL"] = "Home";
        $this->addBreadCrumb("Home", site_url("home"));
        $this->addBreadCrumb("Login", site_url("login"));
        $this->_renderPage("login");
    }

    public function register() {
        redirect("home/error");
        $this->_data["menuHL"] = "Home";
        $this->addBreadCrumb("Home", site_url("home"));
        $this->addBreadCrumb("Register", site_url("login/register"));
        $this->_renderPage("register");
    }

    public function doRegister() {
        $this->load->helper('string');
        $this->load->helper('email');
        $this->load->library('email');
        $email = $this->input->post("email");
        if (valid_email($email)) {
            // check last token
            $this->load->database("homepage");
            $this->db->from("registration");
            $this->db->where("email", $email);
            $query = $this->db->get();
            $row = $query->row();

            if ($row == NULL) {
                // generate token
                $token = random_string('alnum', 20);
                $message = $token;

                // send email
                $this->email->clear(true);
                $this->email->from('noreply@perdesaansehat.or.id', 'Perdesaan Sehat');
                $this->email->to($email);

                $this->email->subject("Aktivasi Akun Perdesaan Sehat");
                $this->email->message($message);

                if ($this->email->send()) {
                    // save to db
                    $data = array(
                        'email' => $email,
                        'token' => $token,
                        'timestamp' => date("Y-m-d H:i:s")
                    );
                    $this->db->insert('registration', $data);
                }
            } else {
                redirect("login/register");
            }
        } else {
            redirect("login/register");
        }
    }

    public function doLogin($isAjax = false) {
        $lastPage = $this->input->post("lastpage");
        $email = $this->input->post("e");
        $pwd = $this->input->post("p");

        $this->load->library('session');
        $this->session->set_userdata('useremail', $email);
        $this->session->set_userdata('userpwd', $pwd);
        $this->_updateLoginSession();

        if (!$isAjax) {
            if ($this->_loggedUser != NULL) {
                if (is_string($lastPage)) {
                    redirect($lastPage);
                } else {
                    redirect("usercp");
                }
            } else {
                redirect("home/error");
            }
        } else {
            echo $this->_loginStatus;
            exit;
        }
    }

    public function doFBLogin($isAjax = false) {
        
    }

    public function doLogout($isAjax = false) {
        $lastPage = $this->input->post("lastpage");

        $this->load->library('session');
        $this->session->unset_userdata('useremail');
        $this->session->unset_userdata('userpwd');

        if (!$isAjax) {
            if (is_string($lastPage)) {
                redirect($lastPage);
            } else {
                redirect("home");
            }
        } else {
            $this->_updateLoginSession();
            echo $this->_loginStatus;
            exit;
        }
    }

}
